var selectedRow = '';
var def_editType = 'add';
var isInit = true;

$(function () {
    regValidate(["#_dictDataForm"]);
    window.actionEvents = {
        'click .infoView': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            infoDictPage();
        },
    }
});

$('#editDictPage').on('hidden.bs.modal', function () {
    // 执行一些动作...
    $('#_dictCode').removeAttrs('disabled');
    resetFormTip('#_dictDataForm');
    resetInputCss('#_dictDataForm');
});

function infoDictPage() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    $('#lab_dictCode').html(selectedRow.dictCode);
    $('#lab_dictName').html(selectedRow.dictName);
    $('#lab_remark').html(selectedRow.remark);
    $('#dictInfoPage').modal('show');
}

function closeDictInfoPage() {
    $('#dictInfoPage').modal('hide');
    resetFormTip('#_dictDataForm1');
}

function doRefresh() {
    selectedRow = '';
    var dictName = $('#dictName').val();
    $('#dataTable').bootstrapTable('refresh', {
        url: basePath + 'sysDictionary/pageFindDictData?t=' + new Date().getTime(),
        query: {
            dictName : dictName,
            groupCode : sysDictCode
        },
    });
}

function getParams(params) {
    var dictName = $('#dictName').val();
    var pageSize = params.pageSize;
    var pageNo = params.pageNumber;
    if (isInit) {
        isInit = false;
        pageSize = defDictInfo.pageSize ? defDictInfo.pageSize : params.pageSize;
        pageNo = defDictInfo.pageNo ? defDictInfo.pageNo : params.pageNumber;
    }
    return {
        dictName : dictName,
        groupCode : sysDictCode,
        pageSize : pageSize,
        pageNo : pageNo
    };
}

function openAddDictPage() {
    def_editType = 'add';
    $('#_modelTitle').html('新增字典');
    $('#_dictCode').val('');
    $('#_dictName').val('');
    $('#remark').val('');
    $('#editDictPage').modal('show');
}

function openEditDictPage() {
    def_editType = 'edit';
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("编辑"));
        return;
    }
    $('#_modelTitle').html('编辑字典');
    $('#_dictCode').val(selectedRow.dictCode);
    $('#_dictCode').attr('disabled', 'disabled');
    $('#_dictName').val(selectedRow.dictName);
    $('#remark').val(selectedRow.remark);
    $('#editDictPage').modal('show');
}

function closeEditDictPage() {
    $('#editDictPage').modal('hide');
    $('#_dictCode').removeAttrs('disabled');
    resetFormTip('#_dictDataForm');
    resetInputCss('#_dictDataForm');
}

function doSaveDict() {
    if(!$("#_dictDataForm").valid()) return;//校验不通过
    var dictCode = $.trim($('#_dictCode').val());
    var dictName = $.trim($('#_dictName').val());
    var remark = $.trim($('#remark').val());
    var postData = {
        groupCode : sysDictCode,
        dictCode : dictCode,
        dictName : dictName,
        remark : remark,
    };
    var url = basePath + 'sysDictionary/addDict';
    if (def_editType == 'edit') {
        url = basePath + 'sysDictionary/editDict';
        postData.id = selectedRow.id;
    } else {
        postData.groupName = sysDictCode;
    }
    $.ajax({
        type: 'post',
        url: url,
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success(common_save_success);
                closeEditDictPage();
                doRefresh();
            }else if(data&&!data.isSuccess){
                if (data.errorCode == 'EXIST') {
                    toastr.error('此字典已存在，无需重复添加');
                    return;
                }
                if (data.errorCode == 'EXIST_CODE') {
                    toastr.error('此字典编码已存在，无法重复添加');
                    return;
                }
                toastr.error(data.errorDesc);
            }else{
                toastr.error(common_save_fail);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function checkDictDelete() {
    if (!selectedRow.id) {
        toastr.error(common_no_choose_msg.format("删除"));
        return;
    }
    var postData = {
        groupCode : selectedRow.dictCode,
    };
    $.ajax({
        type: 'post',
        url: basePath + 'sysDictionary/checkDictDelete',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if (!data) {
                toastr.error('校验删除失败');
                return;
            }
            if (!data.isSuccess) {
                toastr.error(data.errorDesc);
                return;
            }
            if (data.displayMsg == 'EXIST') {
                toastr.warning('当前字典下有枚举，无法删除，请删除枚举后重试。');
            } else {
                var strHtml = '是否删除字典【'+selectedRow.dictName+'】？该操作会变更字典数据！';
                swalToastr('', strHtml, 'warning', deleteDict);
            }
            return;
        },
    });
}

function deleteDict() {
    var postData = {
        id : selectedRow.id,
        groupCode : selectedRow.groupCode,
        dictCode : selectedRow.dictCode,
        status : 'delete'
    };
    $.ajax({
        type: 'post',
        url: basePath + 'sysDictionary/deleteDict',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success('删除成功');
                doRefresh();
            }else if(data&&!data.isSuccess){
                toastr.error(data.errorDesc);
            }else{
                toastr.error('删除失败');
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function openEnumPage() {
    if (!selectedRow || selectedRow == '') {
        toastr.error(common_no_choose_msg.format("查看枚举"));
        return;
    }
    var tableOptions = $('#dataTable').bootstrapTable('getOptions', null);
    window.location.href = basePath + 'sysDictionary/enumPage?groupCode=' + selectedRow.dictCode +
        '&groupName=' + encodeURI(encodeURI(selectedRow.dictName)) + '&' +$('#searchForm').serialize() +
        '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}

//左边选中事件
$("#dataTable").on("click-row.bs.table",function(e,row,ele){
    $(".success").removeClass("success");
    $(ele).addClass("success");
    selectedRow = row;
});
